package com.ece.devirtualizus.Dao;

import java.io.File;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class SqlLiteProgressBarHelper extends SQLiteOpenHelper {

	Context context;
	private static final String DATABASE_NAME = "progressbar1.db";
	private static final int DATABASE_VERSION = 1;

	public static final String TABLE_PROGRESS_BAR_NIVEAU = "progressBarObjectifNiveau";
	public static final String COLUMN_NIVEAU_ID = "id";
	public static final String COLUMN_NIVEAU_ID_TYPE = "idProgress";
	public static final String COLUMN_NIVEAU_VALUE = "niveauValue";
	public static final String COLUMN_NIVEAU_OBJECTIF = "objectif";

	public static final String TABLE_PROGRESS_BAR_TYPE = "progressBarType";
	public static final String COLUMN_TYPE_ID = "id";
	public static final String COLUMN_TYPE_LIBELLE = "progressBarLibelle";

	public static final String TABLE_PROGRESS_BAR_MODULE_TYPE = "moduleType";
	public static final String COLUMN_MODULE_TYPE_ID = "id";
	public static final String COLUMN_MODULE_TYPE_LIBELLE = "moduleLibelle";

	public static final String TABLE_PROGRESS_BAR_MODULE_SEGMENTATION = "moduleSegmentation";
	public static final String COLUMN_MODULE_SEGMENTATION_ID = "id";
	public static final String COLUMN_MODULE_SEGMENTATION_ID_MODULE_TYPE = "idModule";
	public static final String COLUMN_MODULE_SEGMENTATION_ID_TYPE = "idProgress";
	public static final String COLUMN_MODULE_SEGMENTATION_COEFF = "coeff";

	public static final String TABLE_PROGRESS_BAR_MODULE_EVOLUTION = "evolutionByModule";
	public static final String COLUMN_MODULE_EVOLUTION_ID = "id";
	public static final String COLUMN_MODULE_EVOLUTION_ID_MODULE = "idModule";
	public static final String COLUMN_MODULE_EVOLUTION_ID_ACTUAL_LEVEL = "idActual";
	public static final String COLUMN_MODULE_EVOLUTION_VALUE = "nbRepetitions";
	public static final String COLUMN_MODULE_EVOLUTION_IS_BACKUPED = "isStoreInBdd";
	public static final String COLUMN_MODULE_EVOLUTION_LAST_UPDATE = "lastUpdate";
	public static final String COLUMN_MODULE_EVOLUTION_DATE_BEGIN_SESSION = "dateBeginSession";
	public static final String COLUMN_MODULE_EVOLUTION_DATE_END_SESSION = "dateEndSession";
	
	public static final String TABLE_PROGRESS_BAR_ACTUAL_LEVEL = "actualLevel";
	public static final String COLUMN_ACTUAL_LEVEL_ID = "id";
	public static final String COLUMN_ACTUAL_LEVEL_PROGRESS_ID = "idProgress";
	public static final String COLUMN_ACTUAL_LEVEL_NIVEAU_ACTUAL = "myLevel";

	private static final String DATABASE_CREATE_TABLE_NIVEAU = "create table IF NOT EXISTS "
			+ TABLE_PROGRESS_BAR_NIVEAU
			+ "("
			+ COLUMN_NIVEAU_ID
			+ " integer primary key autoincrement, "
			+ COLUMN_NIVEAU_ID_TYPE
			+ " integer not null, "
			+ COLUMN_NIVEAU_VALUE
			+ " integer not null, "
			+ COLUMN_NIVEAU_OBJECTIF
			+ " integer not null);";

	private static final String DATABASE_CREATE_TABLE_TYPE = "create table IF NOT EXISTS "
			+ TABLE_PROGRESS_BAR_TYPE
			+ "("
			+ COLUMN_TYPE_ID
			+ " integer primary key autoincrement, "
			+ COLUMN_TYPE_LIBELLE
			+ " TEXT not null);";

	private static final String DATABASE_CREATE_TABLE_MODULE_TYPE = "create table IF NOT EXISTS "
			+ TABLE_PROGRESS_BAR_MODULE_TYPE
			+ "("
			+ COLUMN_MODULE_TYPE_ID
			+ " integer primary key autoincrement, "
			+ COLUMN_MODULE_TYPE_LIBELLE + " TEXT not null);";

	private static final String DATABASE_CREATE_TABLE_MODULE_SEGMENTATION = "create table IF NOT EXISTS "
			+ TABLE_PROGRESS_BAR_MODULE_SEGMENTATION
			+ "("
			+ COLUMN_MODULE_SEGMENTATION_ID
			+ " integer primary key autoincrement, "
			+ COLUMN_MODULE_SEGMENTATION_ID_MODULE_TYPE
			+ " integer not null, "
			+ COLUMN_MODULE_SEGMENTATION_ID_TYPE
			+ " integer not null, "
			+ COLUMN_MODULE_SEGMENTATION_COEFF + " REAL not null);";

	private static final String DATABASE_CREATE_TABLE_MODULE_EVOLUTION = "create table IF NOT EXISTS "
			+ TABLE_PROGRESS_BAR_MODULE_EVOLUTION
			+ "("
			+ COLUMN_MODULE_EVOLUTION_ID
			+ " integer primary key autoincrement, "
			+ COLUMN_MODULE_EVOLUTION_ID_MODULE
			+ " integer not null, "
			+ COLUMN_MODULE_EVOLUTION_ID_ACTUAL_LEVEL
			+ " integer not null, "
			+ COLUMN_MODULE_EVOLUTION_VALUE
			+ " integer not null, "
			+ COLUMN_MODULE_EVOLUTION_LAST_UPDATE
			+ " TEXT not null, "
			+ COLUMN_MODULE_EVOLUTION_DATE_BEGIN_SESSION
			+ " TEXT not null, "
			+ COLUMN_MODULE_EVOLUTION_DATE_END_SESSION
			+ " TEXT not null, "
			+ COLUMN_MODULE_EVOLUTION_IS_BACKUPED + " integer not null);";

	private static final String DATABASE_CREATE_TABLE_ACTUAL_LEVEL = "create table IF NOT EXISTS "
			+ TABLE_PROGRESS_BAR_ACTUAL_LEVEL
			+ "("
			+ COLUMN_ACTUAL_LEVEL_ID
			+ " integer primary key autoincrement, "
			+ COLUMN_ACTUAL_LEVEL_PROGRESS_ID
			+ " integer not null, "
			+ COLUMN_ACTUAL_LEVEL_NIVEAU_ACTUAL + " integer not null);";

	/*
	 * private static final String DATABASE_CREATE_TABLE_SUIVI =
	 * "create table IF NOT EXISTS " + TABLE_PROGRESS_BAR_SUIVI + "(" +
	 * COLUMN_SUIVI_ID + " integer primary key autoincrement, " +
	 * COLUMN_SUIVI_NIVEAU + " integer not null, " + COLUMN_SUIVI_LAST_VALUE +
	 * " integer not null, " + COLUMN_SUIVI_LAST_UPDATE + " TEXT not null);";
	 */

	public SqlLiteProgressBarHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
		this.context = context;
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL(DATABASE_CREATE_TABLE_NIVEAU);
		//Log.i("DATABASE_CREATE", DATABASE_CREATE_TABLE_NIVEAU);

		db.execSQL(DATABASE_CREATE_TABLE_TYPE);
		//Log.i("DATABASE_CREATE", DATABASE_CREATE_TABLE_TYPE);

		db.execSQL(DATABASE_CREATE_TABLE_MODULE_TYPE);
		//Log.i("DATABASE_CREATE", DATABASE_CREATE_TABLE_MODULE_TYPE);

		db.execSQL(DATABASE_CREATE_TABLE_MODULE_SEGMENTATION);
		//Log.i("DATABASE_CREATE", DATABASE_CREATE_TABLE_MODULE_SEGMENTATION);

		db.execSQL(DATABASE_CREATE_TABLE_MODULE_EVOLUTION);
		//Log.i("DATABASE_CREATE", DATABASE_CREATE_TABLE_MODULE_EVOLUTION);

		db.execSQL(DATABASE_CREATE_TABLE_ACTUAL_LEVEL);
		//Log.i("DATABASE_CREATE", DATABASE_CREATE_TABLE_ACTUAL_LEVEL);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_PROGRESS_BAR_ACTUAL_LEVEL
				+ ";");
		Log.i("onUpgrade", "DROP TABLE IF EXISTS "
				+ TABLE_PROGRESS_BAR_ACTUAL_LEVEL + ";");

		db.execSQL("DROP TABLE IF EXISTS "
				+ TABLE_PROGRESS_BAR_MODULE_EVOLUTION + ";");
		Log.i("onUpgrade", "DROP TABLE IF EXISTS "
				+ TABLE_PROGRESS_BAR_MODULE_EVOLUTION + ";");

		db.execSQL("DROP TABLE IF EXISTS "
				+ TABLE_PROGRESS_BAR_MODULE_SEGMENTATION + ";");
		Log.i("onUpgrade", "DROP TABLE IF EXISTS "
				+ TABLE_PROGRESS_BAR_MODULE_SEGMENTATION + ";");

		db.execSQL("DROP TABLE IF EXISTS " + TABLE_PROGRESS_BAR_MODULE_TYPE
				+ ";");
		Log.i("onUpgrade", "DROP TABLE IF EXISTS "
				+ TABLE_PROGRESS_BAR_MODULE_TYPE + ";");

		db.execSQL("DROP TABLE IF EXISTS " + TABLE_PROGRESS_BAR_NIVEAU + ";");
		Log.i("onUpgrade", "DROP TABLE IF EXISTS " + TABLE_PROGRESS_BAR_NIVEAU
				+ ";");

		db.execSQL("DROP TABLE IF EXISTS " + TABLE_PROGRESS_BAR_TYPE + ";");
		Log.i("onUpgrade", "DROP TABLE IF EXISTS " + TABLE_PROGRESS_BAR_TYPE
				+ ";");
		// db.execSQL("DROP TABLE IF EXISTS " + DATABASE_CREATE_TABLE_SUIVI +
		// ";");
		// Log.i("onUpgrade", "DROP TABLE IF EXISTS "
		// + DATABASE_CREATE_TABLE_SUIVI + ";");

		onCreate(db);
	}

	public void destroyDatabase(SQLiteDatabase db) {
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_PROGRESS_BAR_ACTUAL_LEVEL
				+ ";");
		Log.i("onDestroy", "DROP TABLE IF EXISTS "
				+ TABLE_PROGRESS_BAR_ACTUAL_LEVEL + ";");

		db.execSQL("DROP TABLE IF EXISTS "
				+ TABLE_PROGRESS_BAR_MODULE_EVOLUTION + ";");
		Log.i("onDestroy", "DROP TABLE IF EXISTS "
				+ TABLE_PROGRESS_BAR_MODULE_EVOLUTION + ";");

		db.execSQL("DROP TABLE IF EXISTS "
				+ TABLE_PROGRESS_BAR_MODULE_SEGMENTATION + ";");
		Log.i("onDestroy", "DROP TABLE IF EXISTS "
				+ TABLE_PROGRESS_BAR_MODULE_SEGMENTATION + ";");

		db.execSQL("DROP TABLE IF EXISTS " + TABLE_PROGRESS_BAR_MODULE_TYPE
				+ ";");
		Log.i("onDestroy", "DROP TABLE IF EXISTS "
				+ TABLE_PROGRESS_BAR_MODULE_TYPE + ";");

		db.execSQL("DROP TABLE IF EXISTS " + TABLE_PROGRESS_BAR_NIVEAU + ";");
		Log.i("onDestroy", "DROP TABLE IF EXISTS " + TABLE_PROGRESS_BAR_NIVEAU
				+ ";");

		db.execSQL("DROP TABLE IF EXISTS " + TABLE_PROGRESS_BAR_TYPE + ";");
		Log.i("onDestroy", "DROP TABLE IF EXISTS " + TABLE_PROGRESS_BAR_TYPE);
	}

	public boolean checkDataBase() {
		File dbFile = context.getDatabasePath("/data/data/com.ece.devirtualizus/databases/progressbar1.db");
		return dbFile.exists();
	}
}
